package com.z.sys.controller;

import com.github.pagehelper.PageInfo;
import com.z.common.web.base.BaseController;
import com.z.common.web.vo.request.PageVo;
import com.z.common.web.vo.response.Result;
import com.z.sys.entity.RolePermission;
import com.z.sys.service.IRolePermissionService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Arrays;

/**
 * 角色资源授权Controller
 *
 * @author z
 * @date 2023-03-02
 */
@RestController
@RequestMapping("/system/rolePermission")
public class RolePermissionController extends BaseController {

    private String prefix = "system/rolePermission";

    @Autowired
    private IRolePermissionService rolePermissionService;

    @GetMapping("/main")
    public ModelAndView main() {
        return jumpPage(prefix + "/main");
    }

    /**
     * 查询角色资源授权列表
     */
    @ResponseBody
    @GetMapping("/data")
    public Result list(@ModelAttribute RolePermission rolePermission, PageVo pageVo) {
        PageInfo<RolePermission> pageInfo = rolePermissionService.selectRolePermissionPage(rolePermission, pageVo);
        return Result.table(pageInfo);
    }

    /**
     * 新增角色资源授权
     */
    @GetMapping("/add")
    public ModelAndView add() {
        return jumpPage(prefix + "/add");
    }

    /**
     * 新增角色资源授权
     */
    @ResponseBody
    @PostMapping("/save")
    public Result save(@RequestBody RolePermission rolePermission) {
        return Result.decide(rolePermissionService.save(rolePermission));
    }

    /**
     * 修改角色资源授权
     */
    @GetMapping("/edit")
    public ModelAndView edit(Long id, ModelMap map) {
        RolePermission rolePermission =rolePermissionService.getById(id);
        map.put("rolePermission", rolePermission);
        return jumpPage(prefix + "/edit");
    }

    /**
     * 修改角色资源授权
     */
    @ResponseBody
    @PutMapping("/update")
    public Result update(@RequestBody RolePermission rolePermission) {
        return Result.decide(rolePermissionService.updateById(rolePermission));
    }

    /**
     * 删除角色资源授权
     */
    @ResponseBody
    @DeleteMapping("/batchRemove")
    public Result batchRemove(String ids) {
        return Result.decide(rolePermissionService.removeByIds(Arrays.asList(ids.split(","))));
    }

    /**
     * 删除角色资源授权
     */
    @ResponseBody
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable("id") Long id) {
        return Result.decide(rolePermissionService.removeById(id));
    }

    /**
     * Describe: 保存操作权限
     * Param RoleId PowerIds
     * Return Result
     */
    @PutMapping("saveRolePower")
    @ApiOperation(value = "保存角色权限数据")
    public Result saveRolePower(String roleId, String powerIds) {
        return Result.decide(rolePermissionService.saveRolePermisson(roleId, Arrays.asList(powerIds.split(","))));
    }

    /**
     * Describe: 获取角色权限
     * Param RoleId
     * Return ResultTree
     */
    @GetMapping("getRolePower")
    @ApiOperation(value = "获取角色权限数据")
    public Result getRolePower(Integer roleId) {
        return new Result(200, rolePermissionService.getRolePermission(roleId));
    }
}
